package base;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 读取文件
 *
 * @author 王霄
 * @date 2021/9/16 15:33
 **/
public class IODemo {
    public static void main(String[] args) throws IOException {
        var filePath = "file.txt";
        read1(filePath);
        read2(filePath);
    }

    private static void read1(String filePath) throws IOException {
        var inputStream = new FileInputStream(filePath);
        var reader = new InputStreamReader(inputStream);
        var chars = new char[1024];
        while (reader.read(chars) != -1) {
            System.out.println(new String(chars));
        }
        reader.close();
    }

    private static void read2(String filePath) throws IOException {
        var reader = new FileReader(filePath);
        var chars = new char[1024];
        while (reader.read(chars) != -1) {
            System.out.println(new String(chars));
        }
        reader.close();
    }
}
